//
//  DNAvoidCrash.swift
//  LunalerApp
//
//  Created by 露乐ios on 2018/2/6.
//  Copyright © 2018年 露乐ios. All rights reserved.
//

import Foundation
import UIKit

extension NSMutableDictionary
{
    func dnSetObject(anObject:AnyObject? , aKey:Any?){
        if (anObject==nil){
            errorHandle(errMsg: "Object为空")
            return
        }
        if(aKey == nil){
            errorHandle(errMsg: "Key为空")
            return
        }
        self.setObject(anObject ?? "", forKey: aKey as! NSCopying);
    }
    
    func dnSetValue(value:AnyObject?,aKey:AnyObject?){
        if (aKey == nil){
            errorHandle(errMsg: "Key为空")
            return
        }
        if !(aKey is String){
            errorHandle(errMsg: "Key 应该是NSString 类型")
            return
        }
        self.setValue(value, forKey: aKey as! String)
    }
    
    func dnRemoveObjectForKey(aKey:AnyObject?){
        if aKey == nil {
            errorHandle(errMsg: "Key为空")
            return
        }
        self.removeObject(forKey: aKey as! String);
    }
}


extension NSArray
{
    func dnObjectAtIndex(idx:NSInteger) -> Any {
        let count = self.count
        if idx >= count {
            errorHandle(errMsg: "数组越界")
            return NSNull()
        }
        return self.object(at: idx) as AnyObject
    }
    
    func dnObjectAtIndexes(indexes:IndexSet) -> Any {
        let count = self.count
        if indexes.last! >= count || indexes.last! >= count{
            errorHandle(errMsg: "数组越界")
            return NSNull()
        }
        return self.objects(at: indexes as IndexSet)
    }
    
    func dnArrayByAddObject(anObject:AnyObject?) -> Any {
        if anObject == nil || anObject is NSNull{
            errorHandle(errMsg: "元素为空")
            return NSNull()
        }
        return self.adding(anObject!) as NSArray
    }
}

extension NSMutableArray
{
    func dnMutableArrayObjectAtIndex(idx: NSInteger) -> Any
    {
        let count = self.count
        if  idx >= count{
            errorHandle(errMsg: "数组越界")
            return NSNull()
        }
        return self.object(at:idx)
    }
    
    func dnMutableArrayRemoveObjectAtIndex(index:NSInteger)
    {
        let count = self.count
        if index >= count {
            errorHandle(errMsg: "数组越界")
            return ;
        }
        self.removeObject(at: index)
    }
    
    func dnMutableArrayInsertObject(anObject:AnyObject,index:NSInteger)
    {
        let count = self.count
        if index > count {
            errorHandle(errMsg: "数组越界")
            return
        }
        if anObject is NSNull {
            errorHandle(errMsg: "元素为空")
            return
        }
        self.insert(anObject, at: index)
    }
    
    func dnMutableArrayAddObject(anObject:AnyObject){
        if anObject is NSNull {
            errorHandle(errMsg: "元素为空")
            return
        }
        self.add(anObject);
    }
}






extension NSAttributedString
{
    
    func dnInitWithString(str:String) -> Any {
        if str.lengthOfBytes(using: .utf8) <= 0 {
            errorHandle(errMsg: "str 为 nil")
            return NSNull()
        }
        return NSAttributedString.init(string: str)
    }
    
    func dnInitWithAttributedString(attrStr:NSAttributedString) -> Any {
        return NSAttributedString.init(attributedString: attrStr)
    }
    
    func dnInitWithStringOrAttributes(str:String,attrs:NSDictionary) -> Any {
        if str.lengthOfBytes(using: .utf8) <= 0{
            errorHandle(errMsg: "str 为 nil")
            return NSNull()
        }
        
        return NSAttributedString.init(string: str, attributes: (attrs as! [NSAttributedStringKey : Any]))
    }
    
    func dnAttributesAtIndexOrEffectiveRange(location:UInt,range:NSRangePointer,rangeLimit:NSRange) -> Any{
        if (location > self.length-1){
            errorHandle(errMsg: "location超出界限")
            return NSNull()
        }
        var dict : NSDictionary
        dict = self.attributes(at: Int(location), longestEffectiveRange: range, in: rangeLimit) as NSDictionary
        return dict;
    }
}

extension NSMutableString
{
    func dnReplaceCharactersInRangeWithString(range:NSRange,string:String){
        if range.location > self.length {
            errorHandle(errMsg: "location beyond")
            return
        }
        
        if range.location + range.length > self.length {
            errorHandle(errMsg: "lenght beyond")
            return
        }
        self.replaceCharacters(in: range, with: string)
    }
    
    
    func dnInsertStringAtIndex(string:String,loc:NSInteger){
        if loc > self.length{
            errorHandle(errMsg: "index beyond")
            return
        }
        self.insert(string, at: loc)
    }
    
    func dnDeleteCharactersInRange(range:NSRange){
        if range.location > self.length {
            errorHandle(errMsg: "location beyond")
            return
        }
        if range.location + range.length > self.length {
            errorHandle(errMsg: "lenght beyond")
            return
        }
        self.deleteCharacters(in: range)
    }
}

extension NSObject
{
    func dnObjSetValueForKey(value:AnyObject,key:String){
        if key.lengthOfBytes(using: .utf8) <= 0{
            errorHandle(errMsg: "key为空")
        }
        
        self.setValue(value, forKey: key)
    }
}

extension NSString
{
    func  dnCharacterAtIndex(index:NSInteger) -> unichar {
        if index > self.length-1 {
            errorHandle(errMsg: "index 越界")
            return 0
        }
        return self.character(at:index)
    }
    
    func dnSubStringFromIndex(from:NSInteger) -> AnyObject {
        if from > self.length - 1 {
            errorHandle(errMsg: "from 超出界限")
            return NSNull()
        }
        return self.substring(from: from) as AnyObject
    }
    
    func dnSubStringToIndex(to:NSInteger) -> AnyObject {
        if to > self.length - 1{
            errorHandle(errMsg: "to 超出界限")
            return self
        }
        return self.substring(to: to) as AnyObject
    }
    
    func dnSubStringWithRange(range:NSRange) -> AnyObject {
        if range.location > self.length - 1 {
            errorHandle(errMsg: "range.location 超出界限")
            return NSNull()
        }
        if range.location + range.length > self.length - 1{
            errorHandle(errMsg: "range.length 超出界限，已进行截取处理")
            return self.dnSubStringFromIndex(from: range.location)
        }
        return self.substring(with: range) as AnyObject
    }
    
    func dnStringByReplacingOccurrencesOfStringwithString(target:String,replacement:String
        ) -> AnyObject {
        
        if target.lengthOfBytes(using: .utf8) > 0 {
            return self.replacingOccurrences(of: target, with: replacement) as AnyObject
        }
        errorHandle(errMsg: "target 字符串为nil")
        return self
    }
}

extension UserDefaults
{
    func dnUserDefaultSetValueForKey(value:AnyObject,key:String){
        if key.lengthOfBytes(using: .utf8) <= 0 {
            errorHandle(errMsg: "key 不可以为空")
            return
        }
        self.setValue(value, forKey: key);
    }
}


